package com.skt.common_hr.teamprofile_sac.model
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.teamprofile_sac.TeamProfileFacade;
	import com.skt.common_hr.teamprofile_sac.model.vo.MemberVO;
	import com.wcl.dataloader.*;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class MemberSearchProxy extends CommonProxy
	{
		public static const NAME:String = "MemberSearchProxy";
		
		public var memberList:ArrayCollection;
		public var msgStr:String;
		
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function MemberSearchProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function load(orgeh:String, keyword:String):void
		{
			var param:Object = new Object();
			param["I_ORGEH"] = orgeh;
			param["I_KEYWORD"] = keyword;
			loadData("Z_HR_0016_A", param, result, fault); 
		}
		
		//성명 검색 ( 성명검색시 결과 메시지 출력 떄문에 펑션 분리 )
		public function nameLoad(orgeh:String, keyword:String, type:String):void
		{
			var param:Object = new Object();
			param["I_ORGEH"] = orgeh;
			
			//13.04.23 사번검색 추가
			if(type == "Name"){
				param["I_KEYWORD"] = keyword;
				param["I_NUMBER"] = "";
			}else{
				param["I_KEYWORD"] = "";
				param["I_NUMBER"] = keyword;
			}
			
			param["I_PERNR"] = Variables.myPernr; // 0630
			loadData("Z_HR_0016_A", param, nameSearchResult, nameSearchFault); 
		}

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function result(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				memberList = parseMemberList(event.result.RTAB);
				facade.sendNotification(TeamProfileFacade.LOAD_MEMBERSEARCH_COMPLETE, this);
			}
			else {
				trace( "failed to get org list: " + event.result["RETCODE"] + ", " + event.result["RETTEXT"] );
				msgStr = event.result["RETTEXT"];
				facade.sendNotification(TeamProfileFacade.LOAD_MEMBERSEARCH_FAILED, this);
			}
			CursorManager.removeBusyCursor();
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function fault(event:FaultEvent):void
		{
			trace(event.fault.faultString);
			msgStr = event.fault.faultString;
			facade.sendNotification(TeamProfileFacade.LOAD_MEMBERSEARCH_FAILED, this);
			CursorManager.removeBusyCursor();
		}
		
		//성명검색 성공
		public function nameSearchResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				memberList = parseMemberList(event.result.RTAB);
				facade.sendNotification(TeamProfileFacade.LOAD_NAMESEARCH_COMPLETE, this);
			}
			else {
				trace( "failed to get org list: " + event.result["RETCODE"] + ", " + event.result["RETTEXT"] );
				msgStr = event.result["RETTEXT"];
				facade.sendNotification(TeamProfileFacade.LOAD_NAMESEARCH_FAILED, this);
			}
			CursorManager.removeBusyCursor();
		}
		
		//성명검색 실패
		private function nameSearchFault(event:FaultEvent):void
		{
			trace(event.fault.faultString);
			msgStr = event.fault.faultString;
			facade.sendNotification(TeamProfileFacade.LOAD_NAMESEARCH_FAILED, this);
			CursorManager.removeBusyCursor();
		}
		
		
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 인사기록 Value Object
		 */
		private function parseMemberList(retObj:ArrayCollection):ArrayCollection
		{
			var list:ArrayCollection = new ArrayCollection();
			for each(var rec:Object in retObj)
			{
				var vo:MemberVO = new MemberVO();
				vo.SEQNR		= rec.SEQNR;		// 기본 소트 순서
				vo.ORGEH 		= rec.ORGEH;		//조직 단위
				vo.STEXT 		= rec.STEXT;		//부서명(상위조직명 포함)
				vo.PERNR 		= rec.PERNR;		//사원번호
				vo.ENAME 		= rec.ENAME;		//사원 또는 지원자의 포맷된 이름
				vo.PLANST 		= rec.PLANST;		//오브젝트이름	( 직책 )
				vo.PTEXT 		= rec.PTEXT;		//사원 하위 그룹 이름 ( 직군 )
				vo.STELLT 		= rec.STELLT;		//오브젝트이름 ( 직무 )
				vo.JWTXT 		= rec.JWTXT;		//텍스트
				vo.TEL_OFFICE 	= rec.TEL_OFFICE;	//회사전화
				vo.TEL_MOBILE1	= rec.TEL_MOBILE1;	//핸드폰 1
				vo.TEL_MOBILE2 	= rec.TEL_MOBILE2;	//핸드폰 2
				vo.WKADDR		= rec.WKADDR;		//근무지 주소
				vo.PHOTO_URL 	= rec.PHOTO_URL;	//사진 URL
				
				if(vo.PHOTO_URL.length<1) {
					vo.PHOTO_URL = "assets/images/noimage.swf";
				}
				vo.BAND 		= rec.BAND;			//BAND
				vo.PERSK 		= rec.PERSK;		//사원하위그룹
				vo.IDATE 		= DateUtil.stringToDateTime(rec.IDATE);		//입사일
				list.addItem(vo);
			}
			return list;
		}
		
	}
}